package com.sora.pet.controller;

import com.sora.basic.exception.BusinessException;
import com.sora.basic.util.LoginContext;
import com.sora.pet.dto.MsgDto;
import com.sora.pet.service.ISearchMasterMsgService;
import com.sora.pet.domain.SearchMasterMsg;
import com.sora.pet.query.SearchMasterMsgQuery;
import com.sora.basic.util.AjaxResult;
import com.sora.basic.util.PageList;
import com.sora.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> pageQuery(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.pageQuery(query);
    }


    /**
     * 用户发布寻主消息的接口
     * @param msg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request){
        try {
            return searchMasterMsgService.publish(msg,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
     * user寻主消息的接口
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.user(query,request);
    }

    /**
     * 寻主消息已经处理的接口
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.finish(query,request);
    }

    /**
     * 寻主池的接口
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.toSearchMasterPool(query,request);
    }

    /**
     * 寻主消息待处理的接口
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query,request);
    }

    /**
     * 店铺拒单的接口
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！");
        }
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto){
        try {
            searchMasterMsgService.accept(msgDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！");
        }
    }
}
